﻿Public Class RegexNamedGroupsViewModel
    Inherits RegexViewModelBase
    Implements IEnumerable(Of RegexNamedGroupViewModel)

#Region "Events"
    ' Insert Event Declarations Here
#End Region

#Region "Properties"
#Region "Constants"
    ' Insert Constant Declarations Here
#End Region

    Private _groups As List(Of Group)
    Public Property Groups() As List(Of Group)
        Get
            Return _groups
        End Get
        Private Set(ByVal value As List(Of Group))
            _groups = value
        End Set
    End Property

    Private _groupViewModels As IEnumerable(Of RegexNamedGroupViewModel)
    Public Property GroupViewModels() As IEnumerable(Of RegexNamedGroupViewModel)
        Get
            Return _groupViewModels
        End Get
        Private Set(ByVal value As IEnumerable(Of RegexNamedGroupViewModel))
            _groupViewModels = value
        End Set
    End Property

    Public ReadOnly Property Count As Integer
        Get
            Return Me.GroupViewModels.Count
        End Get
    End Property

    Public Overrides ReadOnly Property Index As Integer
        Get
            If Me.Groups.Count > 0 Then
                Return Me.Groups(0).Index
            Else
                Return 0
            End If
        End Get
    End Property

    Public Overrides ReadOnly Property Length As Integer
        Get
            If Me.Groups.Count > 0 Then
                Return Me.Groups(0).Length
            Else
                Return 0
            End If
        End Get
    End Property
#End Region

#Region "Constructors"
    Public Sub New(groups As GroupCollection, regex As Regex, viewNavigator As IViewNavigator)
        MyBase.New(regex, viewNavigator)

        Dim l_groups = (
                From n In regex.GetGroupNames()
                Where (
                    Not TypeOf groups(n) Is Match AndAlso
                    regex.GroupNumberFromName(n).ToString() <> n
                )
                Select
                    Name = n,
                    Group = groups(n)
            ).ToList()

        Me.Groups = (
                From g In l_groups
                Select g.Group
            ).ToList()
        Me.GroupViewModels = (
                From g In l_groups
                Select New RegexNamedGroupViewModel(g.Name, g.Group, regex, viewNavigator)
            ).AsEnumerable()
    End Sub
#End Region

#Region "Methods"
    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of RegexNamedGroupViewModel) Implements System.Collections.Generic.IEnumerable(Of RegexNamedGroupViewModel).GetEnumerator
        Return Me.GroupViewModels.GetEnumerator()
    End Function

    Private Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        Return GetEnumerator()
    End Function

#Region "Event Raiser Methods"
    ' Insert Method Declarations which Raise an Event Here
#End Region

#Region "Event Handlers"
    ' Insert Method Declarations which Handle an Event Here
#End Region
#End Region

End Class
